<?php
/**
 * 登录相关总控制器
 * 仲伟涛 2011-3
 */
class Resume_Page_Login extends Resume_Page_Abstract
{
    private $ucenterDb;
    private $errorMsgArr = array(
                    '1' => '用户不存在!',
                    '2' => '密码错误!',
                    '3' => '验证码错误!',
                );
    public function validate(ME_Request $input, ME_Response $output)
    {
	$redirect_url = $input->request('redirect_url');
	$redirect_url = !empty($redirect_url) ? $redirect_url : '';
	$output->redirect_url =
	$input->redirect_url = $redirect_url;
	if($redirect_url){
		setcookie("redirect_url",$redirect_url,time()*3600,'/');
	}

        return true;
    }
    public function loadDb(){
        $this->ucenterDb = Db_Ucenter::instance();
    }
    public function doDefault(ME_Request $input, ME_Response $output)
    {
        //获得登录状态
        $input->jumpLogin = false;
        $loginArr = Libs_UcUser::checkLogin($input,$output);
        if((int)$loginArr['uid']){
            header("Location: ?c=MyResume");
            exit;
        }
        $output->errorMsg = $input->get("msgCode");
        //顶部&底部
        $output->header = Resume_Plugin_Header::getHeader($input, $output);
        $output->footer = Resume_Plugin_Header::getFooter($input, $output);
        //产生验证码
        $checkCode   = new ME_Seccode();
        $authKey     = $checkCode->getAuthKey();
        $rand        = rand(100000, 999999); 
		$output->checkCodeHide  = $rand;
        
        $output->setTemplate('Login');
    }

    /**
     * 执行登录
     *
     */
    public function doLogin(ME_Request $input, ME_Response $output){
        $userName = trim($input->post("userName"));
        $passWord = trim($input->post("passWord"));
        $checkCodeHide = (int)$input->post("checkCodeHide");
        $checkCode     = trim($input->post("checkcode"));
        $rememberme    = (int)$input->post("rememberme");

        
        //获得真正码
        $checkCodeObj   = new ME_Seccode();        
        $trueCode    = $checkCodeHide - 10;
        $checkCodeObj->seccodeconvert($trueCode);
        if(strtoupper($checkCode) != $trueCode){
            $this->goRetry('验证码错误！');
        }

        $paramArr = array('username'=>$userName,'password'=>$passWord);
        $loginResult = Libs_UcUser::synLogin($paramArr);
        if($loginResult['uid'] > 0){
            $paramArr['uid'] = (int)$loginResult['uid'];
            $paramArr['life'] = $rememberme ? 86400 * 360 : 0;
            Libs_UcUser::setLoginStatus($paramArr);
	
            //同步登录其他应用
            echo $loginResult['synJsHtml'];
	    if($input->redirect_url){
		setcookie("redirect_url","",time()-3600,"/");
            	echo '<script>window.location.href="'.$input->redirect_url.'";</script>';
	    }else{
            	echo '<script>window.location.href="index.php?c=MyResume"</script>';
	    }
            exit;
        }else{//登录失败
            $this->goRetry($loginResult['msg']);
        }
        
    }
    //退出
    public function doLogout(ME_Request $input, ME_Response $output){
        Libs_UcUser::clearLoginStatus($input,$output);
        //同步退出其他应用
        echo Libs_UcUser::synLogOut();
        echo '<script>window.location.href="?c=Login";</script>';
        exit;
    }

    //注册
    public function goRetry($code){
        header("Location: ?c=Login&msgCode=" . urlencode($code));
        exit;
    }

}
